package com.validator.controller;

import com.validator.entity.EquipmentType;
import com.validator.entity.Largess;
import com.validator.entity.Order;
import com.validator.entity.PhonePrefix;
import com.validator.service.IFileUploaderService;
import com.validator.service.IOrderValidatorService;
import com.validator.util.Page;
import com.validator.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/api")
public class ValidateController {

    @Autowired
    private IOrderValidatorService orderValidatorService;

    @Autowired
    private IFileUploaderService fileUploaderService;

    @CrossOrigin
    @GetMapping("/orders")
    public Result<Page<Order>> listByPage(
            @RequestParam(required = false) String orderNumber,
            @RequestParam(required = false) String equipmentType,
            @RequestParam(required = false) String sn,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String email,
            @RequestParam(required = false) String phoneNumber,
            @RequestParam(required = false) String validateTime,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String orderBy,
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "20") int pageSize) {
        return orderValidatorService.listByPage(
                orderNumber, equipmentType, sn, name, email, phoneNumber, validateTime, status, orderBy, page, pageSize);
    }

    @CrossOrigin
    @PostMapping("/addOrder")
    public Result<Order> addOrder(String orderNumber) {
        return orderValidatorService.addOrder(orderNumber);
    }

    @CrossOrigin
    @PostMapping("/addOrderByBatch")
    public Result<List<Order>> addOrderByBatch(String[] orderNumbers) {
        return orderValidatorService.addOrderByBatch(orderNumbers);
    }

    @CrossOrigin
    @GetMapping("/phonePrefixList")
    public Result<List<PhonePrefix>> phonePrefixList() {
        return orderValidatorService.listPhonePrefix();
    }

    @CrossOrigin
    @GetMapping("/equipmentTypes")
    public Result<List<EquipmentType>> equipmentTypes() {
        return orderValidatorService.listEquipmentType();
    }

    @CrossOrigin
    @PostMapping("/addEquipmentType")
    public Result<EquipmentType> addEquipmentType(String type, int status) {
        return orderValidatorService.addEquipmentType(type, status);
    }

    @CrossOrigin
    @GetMapping("/orderSelect")
    public Result<Order> orderSelect(String equipmentType, String orderNumber) {
        return orderValidatorService.validateByEquipmentTypeAndOrderNumber(equipmentType, orderNumber);
    }

    @CrossOrigin
    @PostMapping("/validateOrder")
    public Result<Order> validateOrder(@RequestBody Order order) {
        return orderValidatorService.validOrder(order);
    }

    @CrossOrigin
    @PostMapping("/bgUploader/{fileName}")
    public Result<Boolean> bgUploader(
            @PathVariable("fileName") String filename,
            @RequestParam("file") MultipartFile multipartFile) {
        return fileUploaderService.bgUploader(filename, multipartFile);
    }

    @CrossOrigin
    @PostMapping("/largessUploader/{equipmentId}/{fileFormat}")
    public Result<Boolean> largessUploader(
            @PathVariable("equipmentId") int equipmentId,
            @PathVariable("fileFormat") String fileFormat,
            @RequestParam("name") String name,
            @RequestParam("file") MultipartFile multipartFile
    ) {
        return fileUploaderService.largessUploader(equipmentId, fileFormat, name, multipartFile);
    }

    @CrossOrigin
    @GetMapping("/largessList")
    public Result<List<Largess>> largessList() {
        return fileUploaderService.listAllEquipmentLargess();
    }

    @CrossOrigin
    @GetMapping("/largess/{equipmentId}")
    public Result<Largess> selectByEquipmentId(@PathVariable("equipmentId") int equipmentId) {
        return fileUploaderService.getLargessByEquipmentId(equipmentId);
    }

    @CrossOrigin
    @PostMapping("/deleteOrder")
    public Result<Boolean> deleteOrder(String orderNumber) {
        return orderValidatorService.deleteOrderByOrderNumber(orderNumber);
    }

    @CrossOrigin
    @PostMapping("/deleteEquipmentType")
    public Result<Boolean> deleteEquipmentType(int id) {
        return orderValidatorService.deleteEquipmentTypeById(id);
    }
}
